package com.xywl.escort.view.record

import android.graphics.Paint
import android.os.Bundle
import android.support.v4.content.ContextCompat
import android.support.v4.widget.SwipeRefreshLayout
import android.support.v7.widget.LinearLayoutManager
import android.view.LayoutInflater
import android.view.View
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.trello.rxlifecycle2.LifecycleTransformer
import com.xywl.escort.R
import com.xywl.escort.adapter.FundRecordsAdapter
import com.xywl.escort.base.BaseActivity
import com.xywl.escort.bean.UserFundsListResp
import com.xywl.escort.widget.HorizontalDividerItemDecoration
import dagger.android.AndroidInjection
import kotlinx.android.synthetic.main.activity_fund_records.*
import timber.log.Timber
import java.util.*
import javax.inject.Inject

@Route(path = "/xy/fund/records")
class FundRecordsActivity : BaseActivity(), FundRecordsContract.View, SwipeRefreshLayout.OnRefreshListener {
    private lateinit var mAdapter: FundRecordsAdapter

    @Inject
    lateinit var mPresenter: FundRecordsContract.Presenter

    private var pageNum = 1
    private var isEnd: Boolean = false
    private val mList = ArrayList<UserFundsListResp.DataBean.ListBean>()

    override fun onCreate(savedInstanceState: Bundle?) {
        AndroidInjection.inject(this)
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_fund_records)

        initView()
        initData()
    }

    private fun initView() {
        showBackButton()
        setTitleText(R.string.tips_fund_records)
        val manager = LinearLayoutManager(this)
        manager.orientation = LinearLayoutManager.VERTICAL
        val paint = Paint()
        paint.strokeWidth = 30f
        paint.color = ContextCompat.getColor(this, R.color.line_color)
        paint.isAntiAlias = true
        recycler_record.addItemDecoration(HorizontalDividerItemDecoration.Builder(this).paint(paint).build())
        recycler_record.layoutManager = manager

        mAdapter = FundRecordsAdapter(R.layout.item_fund_records, mList)
        recycler_record.adapter = mAdapter

        val emptyView = LayoutInflater.from(this).inflate(R.layout.layout_empty_view, null)
        mAdapter.emptyView = emptyView

        swipeLayout.setOnRefreshListener(this)
        swipeLayout.setColorSchemeResources(R.color.colorAccent, R.color.colorPrimary, R.color.aid_prompt_color, R.color.colorBackground)
        mAdapter.setOnLoadMoreListener({
            swipeLayout.isEnabled = false
            recycler_record.postDelayed({
                swipeLayout.isEnabled = false
                if (isEnd) {
                    //数据全部加载完毕
                    mAdapter.loadMoreEnd()
                    swipeLayout.isEnabled = true
                } else {
                    //成功获取更多数据
                    mPresenter.loadFundRecords(pageNum)
                }
            }, 200)
        }, recycler_record)

        mAdapter.setOnItemClickListener { adapter, view, position ->
            val listBean = mList[position]
            ARouter.getInstance()
                    .build("/xy/fund/records/detail")
                    .withObject("listBean", listBean)
                    .navigation()
        }
    }

    private fun initData() {
        mAdapter.setEnableLoadMore(false)
        pageNum = 1
        mPresenter.takeView(this)
        mPresenter.loadFundRecords(pageNum)
    }

    override fun isActive(): Boolean {
        return false
    }

    override fun <T> bindToLife(): LifecycleTransformer<T> {
        return this.bindToLifecycle()
    }

    override fun onRefresh() {
        mAdapter.setEnableLoadMore(false)
        pageNum = 1
        mPresenter.loadFundRecords(pageNum)
    }

    override fun loadFundRecordsSuccess(dataBean: UserFundsListResp.DataBean?) {
        swipeLayout.isRefreshing = false
        progress_bar.visibility = View.GONE
        recycler_record.visibility = View.VISIBLE

        if (pageNum == 1) {
            mList.clear()
        }
        mList.addAll(dataBean!!.list)
        if (pageNum >= dataBean.pages) {
            isEnd = true
        } else {
            isEnd = false
            pageNum += 1
        }
        mAdapter.setNewData(mList)
        mAdapter.loadMoreComplete()
        swipeLayout.isEnabled = true
    }

    override fun loadFundRecordsFailure() {
        Timber.tag("FundRecordsActivity").e("loadFundRecordsFailure")
        swipeLayout.isRefreshing = false
        progress_bar.visibility = View.GONE
        recycler_record.visibility = View.VISIBLE
    }
}
